﻿namespace Votations.NSurvey.SQLServerDAL
{
    using System;
    using Votations.NSurvey.Data;
    using Votations.NSurvey.IDAL;
    using Microsoft.ApplicationBlocks.Data;
    using System.Data;
    using System.Data.SqlClient;

    public class SurveyIPRange :ISurveyIPRange
    {
        #region ISurveyIPRange Members

        public SurveyIPRangeData GetAllSurveyIPRanges(int surveyID)
        {
            SurveyIPRangeData surveyIPRangeData = new SurveyIPRangeData();

            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spIPRangeGetForSurvey",surveyIPRangeData,
                new string[]{"surveyIPRange"},new SqlParameter[]{new SqlParameter("@SurveyId",surveyID)});

            return surveyIPRangeData;
        }

        public void UpdateSurveyIPRange(SurveyIPRangeData updIPRange)
        {
            SqlConnection connection = new SqlConnection(SqlHelper.DbConnectionString);
            SqlCommand command = new SqlCommand("vts_spSurveyIPRangeUpdate", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@SurveyIPRangeID", updIPRange.SurveyIPRange[0].SurveyIPRangeId));
            command.Parameters.Add(new SqlParameter("@SurveyID", updIPRange.SurveyIPRange[0].SurveyId));
            command.Parameters.Add(new SqlParameter("@IPStart", updIPRange.SurveyIPRange[0].IPStart));
            command.Parameters.Add(new SqlParameter("@IPEnd", updIPRange.SurveyIPRange[0].IPEnd));

            SqlHelper.UpdateDataset(command, new SqlCommand(), new SqlCommand(),updIPRange, "SurveyIPRange", true);
        }

        public void AddNewSurveyIPRange(SurveyIPRangeData updIPRange)
        {
            SqlConnection connection = new SqlConnection(SqlHelper.DbConnectionString);
            SqlCommand command = new SqlCommand("vts_spSurveyIPRangeAddNew", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@SurveyID", updIPRange.SurveyIPRange[0].SurveyId));
            command.Parameters.Add(new SqlParameter("@IPStart", updIPRange.SurveyIPRange[0].IPStart));
            command.Parameters.Add(new SqlParameter("@IPEnd", updIPRange.SurveyIPRange[0].IPEnd));

            SqlHelper.UpdateDataset(command, new SqlCommand(), new SqlCommand(), updIPRange, "SurveyIPRange", true);
        }

        public void DeleteSurveyIPRangeById(int surveyIPId)
        {
                    
            SqlParameter[] spParameters = new SqlParameter[1];
			spParameters[0]	 = new SqlParameter("@SurveyIPRangeId", surveyIPId);
			
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spSurveyIPRangeDelete", spParameters);
        }
        

        #endregion
    }
}
